package com.tcc.qa.test.util;

import org.apache.xpath.operations.String;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import tcc.utils.StringUtil;
import tcc.utils.xml.dom.DOMUtil;
import tcc.utils.xml.xpath.XmlOperator;

import com.tcc.task.CreateTask;

public class FileDataSource {
  public static Object[][] getDatas(Class a, String filepath) {
    String path = filepath;
    path = StringUtil.replaceString(path, "\\", "/");
    Document doc;
    int rowslen = 0;
    Object[][] ret = null;
    try {
      doc = DOMUtil.inputStreamDoc(a.getResourceAsStream(path));

      NodeList rows = XmlOperator.selectNodeList(doc, "//row");

      rowslen = rows != null ? rows.getLength() : 0;
      ret = new Object[rowslen][];
      for (int i = 0; i < rowslen; i++) {
        NodeList cols = XmlOperator.selectNodeList(rows.item(i), "col");
        int collen = cols != null ? cols.getLength() : 0;
        Object[] colsObj = new Object[collen];
        ret[i] = colsObj;
        for (int f = 0; f < collen; f++) {
          colsObj[f] = XmlOperator.getNodeValue(cols.item(f));
        }
      }
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();

    }
    if (ret == null) {
      ret = new Object[rowslen][];
    }
    return ret;
  }

  public static void main(String[] a) {
    try {
      Object[][] v = FileDataSource.getDatas(CreateTask.class, "tasks.xml");
      System.out.println(v);
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}
